package com.veo.dao;

import com.veo.bean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserDao {

    @Select("select * from user")
    @Results( id = "userMap",
            value = {
            @Result(id = true,column = "id",property = "userId"),
            @Result(column = "username",property = "userName"),
            @Result(column = "address",property = "userAddress"),
            @Result(column = "birthday",property = "userBirthday"),
            @Result(column = "sex",property = "userSex"),
            @Result(column = "id",property = "account",many = @Many(select = "com.veo.dao.AccountDao.findById",fetchType = FetchType.LAZY))
    })
    List<User> findAllUsers();

    @Select("select * from user where id = #{id}")
    @ResultMap("userMap")
    User findById(int id);

    @Insert("insert into user(username,sex,birthday,address) values(#{userName},#{userSex},#{userBirthday},#{userAddress})")
    @SelectKey(keyColumn = "id",keyProperty = "userId",resultType =Integer.class,statement = "select last_insert_id()", before = false)
    int addUser(User user);

    @Delete("delete from user where id = #{i}")
    int deleteUser(int id);

    @Update("update user set username = #{userName},address=#{userAddress},sex=#{userSex},birthday=#{userBirthday} where id = #{userId}")
    int updateUser(User user);

    @Select("select count(*) from user")
    int findTotal();
}
